eureka-server
引入依赖

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

application.properties

server.port=8761
# eureka实例的主机名
eureka.instance.hostname=eureka-server
# 不把自己注册到eureka上
eureka.client.register-with-eureka=false
# 不从eureka获取服务的注册信息
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
package com.springboot.eurekaserver;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
/**
 * 注册中心
 * 1.配置Eureka信息
 * 2.@EnableEurekaServer
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}
 
}

访问 http://localhost:8761/ 如下图

privider-ticket
引入依赖

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

application.properties

server.port=8001
spring.application.name=provider-ticket
 
# 注册服务的时候,使用服务的ip地址
eureka.instance.prefer-ip-address=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

TicketService

package com.springboot.prividerticket.service;
 
import org.springframework.stereotype.Service;
 
@Service
public class TicketService {
    public String getTicket(){
	    // provider-ticket-0.0.1-SNAPSHOT8001.jar str为   《廊桥遗梦》8001
		// provider-ticket-0.0.1-SNAPSHOT8002.jar str为   《廊桥遗梦》8002 
        return "${str}";   
    }
}

TicketController

package com.springboot.prividerticket.controller;
 
import com.springboot.prividerticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class TicketController {
 
    @Autowired
    TicketService ticketService;
 
    @GetMapping("/ticket")
    public String getTicket(){
        return ticketService.getTicket();
    }
}

maven package打包,jar -jar运行jar包:

$ java -jar provider-ticket-0.0.1-SNAPSHOT8001.jar
$ java -jar provider-ticket-0.0.1-SNAPSHOT8002.jar


consumer-user
引入依赖

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

application.properties

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

入口类

package com.springboot.consumeruser;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
 
// 开启发现服务功能
@EnableEurekaClient
@SpringBootApplication
public class ConsumerUserApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(ConsumerUserApplication.class, args);
	}
 
	@LoadBalanced  // 使用负载均衡机制
	@Bean
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}
 
}

UserController消费生产者提供的服务~

package com.springboot.consumeruser.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
@RestController
public class UserController {
 
    @Autowired
    RestTemplate restTemplate;
 
    @GetMapping("buyticket")
    public String getTicket(String name){
        String s = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);
        return name+"购买了"+s;
    }
}

访问http://localhost:8200/buyticket?name=王小明 输出:
王小明购买了《廊桥遗梦》8002

王小明购买了《廊桥遗梦》8001

发表评论

邮箱地址不会被公开。 必填项已用*标注