springcloud之eureka
2019-05-17 本文已影响0人
一个神奇的女码农
废话不多说开搞:
先创建一个service 工程:
pom 依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
启动类文件加上注解:@EnableEurekaServer
启动类代码如下:
@SpringBootApplication
@EnableEurekaServer
@PropertySource(value={"file:${INFOSYS_CONFIG}/eurekaserver.properties"})
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
配置文件:
spring.application.name=spring-cloud-eureka
server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
启动后发现报错了
一顿操作后发现是boot 与cloud 版本不一样 将版本改成最新的或者pom 添加:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
启动成功之后访问:http://localhost:8000,就能查看服务页面
![](https://img.haomeiwen.com/i11481798/a6540de98b409962.png)
当application下面显示No instances available,说明目前没有服务
再建一个producer工程
pom 依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置文件如下:
spring.application.name=spring-cloud-admin
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
启动类代码如下:
@SpringBootApplication
@PropertySource(value={"file:${INFOSYS_CONFIG}/eurekaproducer.properties"})
@EnableEurekaClient
@ComponentScans({@ComponentScan("com.rt.platform.business.**")})
public class EurekaproducerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaproducerApplication.class, args);
}
}
服务启动成功之后,访问eureka页面可以看到服务注册上去了:
![](https://img.haomeiwen.com/i11481798/bf89392ad8b5a618.png)
编写一个可调用接口:
controller 代码如下:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "你好 "+name+",欢迎来到cloud全家桶";
}
}
再次启动服务并访问接口:http://localhost:9000/hello?name=hanmeimei
可以看到接口访问成功,页面展示:
![](https://img.haomeiwen.com/i11481798/8ae18c73f791486c.png)
接下来就是consumer工程了
pom 依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置文件如下:
spring.application.name=spring-cloud-consumer
server.port=9005
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
启动类代码如下:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@PropertySource(value={"file:${INFOSYS_CONFIG}/eurekaconsumer.properties"})
@ComponentScans({@ComponentScan("com.*.platform.business.**")})
public class EurekaconsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaconsumerApplication.class, args);
}
}
然后就是调用服务了,
调用服务就比较简单了,只需要在service 接口上添加 注解@FeignClient,加上你需要调用服务的服务名
service代码如下:
@Component
@FeignClient(value= "SPRING-CLOUD-ADMIN")
public interface HelloService {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
String hello(@RequestParam(value = "name") String name);
}
在写一个controller,代码如下:
@RestController
public class ConsumerController {
@Resource
HelloService helloService ;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return helloService .hello(name);
}
}
启动controller服务,发现报错了HelloService 获取不到,那就肯定是没扫描到包,然后在启动类注解后面添加路径:
@EnableFeignClients(basePackages = {"com.*.platform.business.service"})
启动成功
访问;http://localhost:9005/hello/%E6%9D%8E%E7%A3%8A
![](https://img.haomeiwen.com/i11481798/65164e88af14fd38.png)
成功!
一个简单的eureka服务就搭好了