springboot集成eureka

分为3个项目来讲解: 注册中心,provider,consumer

注册中心

注册中心pom.xml添加:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

再添加(注意这个version目前是Hoxton.SR1,以后升级了可能会变):

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Hoxton.SR1</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

application.yml

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server  #eureka实例的主机名
  client:
    register-with-eureka: false #不注册本身到Eureka注册中心
    fetch-registry: false #不从Eureka注册中心获取服务的注册信息
    service-url:
      defaultZone: http://localhost:8761/eureka/

EurekaSpringBootApplication代码:

@EnableEurekaServer//启用Eureka服务
@SpringBootApplication
public class EurekaSpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaSpringBootApplication.class, args);
    }
}

启动项目,访问:http://localhost:8761/ 即可看到注册中心页面。

provider项目

除了注册中心刚才添加的,pom.xml额外添加:

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

application.yml代码:

#启动端口
server:
  port: 8001
#项目名称
spring:
  application:
    name: provider
#eureka配置
eureka:
  instance:
    prefer-ip-address: true #使用服务的id地址注册
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

ProviderSpringBootApplication代码:

@EnableEurekaServer//启用Eureka服务
@SpringBootApplication
public class ProviderSpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderSpringBootApplication.class, args);
    }
}

TicketService代码:

@Service
public class TicketService {
    public String getTicket(){
        return "动车票";
    }
}

TicketController代码:

@RestController
public class TicketController {
    @Autowired
    TicketService ticketService;
    @GetMapping("/ticket")
    public  String getTicket(){
        return  ticketService.getTicket();
    }
}

启动项目。

consumer

pom.xml参照刚才server和client都要。
application.yml代码:

server:
  port: 8002
spring:
  application:
    name: consumer
eureka:
  instance:
    prefer-ip-address: true #使用服务的id地址注册
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

ConsumerSpringBootApplication代码:

@EnableEurekaServer//启用Eureka服务
@SpringBootApplication
public class ConsumerSpringBootApplication {
    @Bean
    @LoadBalanced//启用负载均衡机制
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(ConsumerSpringBootApplication.class, args);
    }
}

UserController代码:

@RestController
public class UserController {
    @Autowired
    RestTemplate restTemplate;
    @GetMapping("/buy")
    public String buyTicket(String name){
        String template = restTemplate.getForObject("http://PROVIDER/ticket", String.class);
        return name+"购买了"+template;
    }
}

启动项目。

效果

注册中心如图(8001是provider,8002是consumer):在这里插入图片描述

浏览器输入: http://localhost:8002/buy?name=zhangsan
返回: zhangsan购买了 动车票

该项目git地址为:
https://github.com/1054294965/boot-eureka

发布了449 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/enthan809882/article/details/104350543