一.搭建服务注册中心:
maven:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
main启动类:
@SpringBootApplication
@EnableEurekaServer
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
1.默认情况下,服务注册中心会尝试把自己也注册成一个服务,所以我们在配置文件中禁止把自己注册成服务
server.port=1111
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
register-with-eureka: false(不向注册中心中注册自己)
fetch-registry: false(不进行服务检索,只进行服务维护)
2.启动main类,访问 http://localhost:1111 即可看到服务注册中心界面
二.高可用服务注册中心
Eureka Server 的设计一开始就考虑了高可用问题,在Eureka的服务治理设计中,所有节点及时服务提供方,也是服务消费方,服务注册中心也不例外。Eureka Server的高可用实际上就是贱自己作为服务想其他服务注册中心注册自己,这样就形成一组互相注册的服务注册中心,以实现服务清单的互相同步,打倒高可用的效果。
三.搭建服务提供者
1.maven:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.main启动类:
@SpringBootApplication
@EnableEurekaClient
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
*在主类中通过加上@EnableEurekaClient注解,激活Eureka中的DiscoveryClient实现(自动化配置,创建DiscoveryClient接口针对Eureka客户端的EurekaDiscoveryClient实例),才能实现上述Controller中对服务信息的输出
3.配置文件
server:
port: 1114
spring:
application:
name: service-hi
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka/
*通过eureka.application.name指定服务名
4.Controller类
@RestController
public class HelloController {
private final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
return "hi " + name + " ,i am from port:" + port;
}
}
启动服务即可在服务注册中心中查看到有一个服务名为SERVICE-HI的服务
四.服务消费者
在spring cloud中 服务发现石油eureka客户端万恒,而服务消费的任务是有ribbon完成
1.maven:
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
2.main启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class ConmuserMain {
@Bean
@LoadBalanced
public RestOperations restTemplate(RestTemplateBuilder builder){
return builder.build();
};
public static void main(String[] args) {
SpringApplication.run(ConmuserMain.class,args);
}
}
*通过@EnableDiscoveryClient注解让该应用注册为Eureka客户端应用,以获得服务发现的能力,同事,在该主类中创建RestTemplate的Spring Bean实例,并通过@LoadBalanced注解开启客户端负载均衡(如果不加入该注解,无法通过服务名访问服务)
3.配置文件
spring:
application:
name: ribbon-consumer
server:
port: 1113
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka/
4.controller、service类
@RestController
public class ConsumerController {
@Autowired
ConsumerService consumerService;
@RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
public String helloConsumer(){
try {
return consumerService.getForEntity();
} catch (Exception e) {
e.printStackTrace();
}
return "ee";
}
}
@Service
public class ConsumerService {
@Autowired
RestOperations restTemplate;
public String getForEntity() throws Exception {
return restTemplate.getForEntity("http://SERVICE-HI/hi",String.class).getBody();
}
}
最后访问http://localhost:1113/hi即可通过服务消费者访问service-hi服务