Consul:一个类似eureka的中间件

1、下载

这里提供了下载的地址,与相关配置

Consul by HashiCorp

可以到官网下载

2、启动服务

 打开控制台,运行

consul agent -dev

这是正常显示页面

 

3、配置springcloud依赖

父类pom.xml包

  <properties>
        <spring.cloud.version>Hoxton.SR12</spring.cloud.version>
        <spring.boot.version>2.3.12.RELEASE</spring.boot.version>
        <mysql.version>8.0.26</mysql.version>
        <mybatis.version>2.2.0</mybatis.version>
        <druid.version>1.2.3</druid.version>
    </properties>


    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>

            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.version}</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            
        </dependencies>
    </dependencyManagement>

4、 配置Consul-provider

 实体类:

import java.util.Date;
import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Id;
import javax.persistence.Table;

/**
 * (Movie)实体类
 *
 * @author makejava
 * @since 2021-12-17 09:12:14
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "movie")
public class Movie implements Serializable {
    private static final long serialVersionUID = -30737887108345962L;
    @Id
    private Integer id;

    private String movieName;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date movieData;


}

mapper 使用了tk.mapper


import com.ycz.springCloud.entity.Movie;
import org.apache.ibatis.annotations.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
 * (Movie)表数据库访问层
 *
 * @author makejava
 * @since 2021-12-17 09:12:13
 */
@Mapper
public interface MovieMapper extends tk.mybatis.mapper.common.Mapper<Movie>,MySqlMapper<Movie> {


}

service:

/**
 * (Movie)表服务接口
 *
 * @author makejava
 * @since 2021-12-17 09:12:14
 */
public interface MovieService {


    Object getAll();

    Object getOne(Integer id);
}

serviceImpl:

import com.ycz.springCloud.mapper.MovieMapper;
import com.ycz.springCloud.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * (Movie)表服务实现类
 *
 * @author makejava
 * @since 2021-12-17 09:12:15
 */
@Service("movieService")
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieMapper movieMapper;


    public Object getAll() {
        return movieMapper.selectAll();
    }

    public Object getOne(Integer id) {
        return movieMapper.selectByPrimaryKey(id);
    }
}

Controller:

import com.ycz.springCloud.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * (Movie)表控制层
 *
 * @author makejava
 * @since 2021-12-17 09:12:12
 */
@RestController
@RequestMapping("movie")
public class MovieController {
    /**
     * 服务对象
     */
    @Autowired
    private MovieService movieService;

    @RequestMapping("getAll")
    public Object getAll(){
       return movieService.getAll();
    }

    @RequestMapping("/getOne/{id}")
    public Object getOne(@PathVariable Integer id){
        return movieService.getOne(id);
    }


}

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient //开启客户端自动发现,注册到consul中
public class Consul8003 {
    public static void main(String[] args) {
        SpringApplication.run(Consul8003.class,args);
    }
}

配置application.yaml

server:
  port: 8003

spring:
  application:
    name: springcloud-concul-provider-8003
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db05?useUnicode=true
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}



mybatis:
  type-aliases-package: com.ycz.com.ycz.com.ycz.springCloud.entity
  configuration:
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mapper/*.xml

运行:

注册到consul中

 

 正常!!!

5、Consul-consumer配置

pom.xml

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <exclusions>
                <exclusion>
                    <groupId>tk.mybatis</groupId>
                    <artifactId>mapper-spring-boot-starter</artifactId>

                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
    </dependencies>

ApplicationConfig

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }


}

 

UserController:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL="http://springcloud-concul-provider-8003";

    @RequestMapping("/getAll")
    public Object getAll(){
        ResponseEntity<Object> forEntity = restTemplate.getForEntity(REST_URL + "/movie/getAll", Object.class);
        return forEntity;
    }

    @RequestMapping("/get/{id}")
    public Object getOne(@PathVariable("id") Integer id){
        ResponseEntity<Object> forEntity = restTemplate.getForEntity(REST_URL + "/movie/getOne/"+id, Object.class);
        return forEntity;
    }


}

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulConsumer9002 {
    public static void main(String[] args) {
        SpringApplication.run(ConsulConsumer9002.class,args);
    }
}

application.yaml

server:
  port: 9002
spring:
  application:
    name: Consul-Consumer-9002
  cloud:
    consul:
      port: 8500
      host: localhost
      discovery:
        service-name: ${spring.application.name} # 注册到consul注册中心的应用名称

启动的:

 然后访问:

http://localhost:9002/user/getAll

正常:

http://localhost:9002/user/get/1

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
_size = enc_hiddens.shape[1] y_t = torch.tensor([self.vocab.tgt['<s>']] * batch_size,搭建一个微服务项目可以分为以下主要步骤: 1. 架构设计:确定微服务架构,包括 device=enc_hiddens.device).unsqueeze(0) # (1, batch_size) h_t, c_t = dec_init_state context = torch.zeros(batch_size, self.hidden_size*2, device=enc_hiddens.device) # (batch_size服务拆分、服务通信、服务治理等方面。 2. 技术选型:选择合适的技术栈, hidden_size*2) # initialize hypothesis hypo = [Hypothesis([self.vocab.tgt['<s>']], ,比如 Spring Cloud、Dubbo、ServiceComb 等。 3. 基础设施搭建:搭建微服务基础设施,包括注册中心、配置中心、网关、链路追踪等。 4. 业务实现:0.0)] completed_hypo = [] for _ in range(max_length): # generate all possible next hypothesis 实现微服务业务逻辑,并进行集成测试、系统测试、性能测试等。 下面我将对每个 all_hypo = [] for h in hypo: if h.value[-1] == self.vocab.tgt['</s>']: 步骤进行详细的解释: 1. 架构设计: 在确定微服务架构时,需要考虑以下方面 completed_hypo.append(h) continue y_emb = self.model_embeddings.target(y_t) # (1, batch_size,: - 服务拆分:将业务功能拆分成不同的服务,每个服务都有独立的职责 embed_size) h_t, c_t, context, attention = self.step(y_emb.squeeze(0), h_t, c_t, enc和功能。 - 服务通信:服务之间需要通过某种方式进行通信,比如 RESTful API、RPC 等_hiddens, context) # h_t: (batch_size, hidden_size), c_t: (batch_size, hidden_size), context。 - 服务治理:需要对服务进行管理和监控,包括服务注册、服务发现、负载均衡: (batch_size, hidden_size*2), attention: (batch_size, src_sent_len) combined_output = self.combined_output、熔断降级等。 2. 技术选型: 在选择技术栈时,需要考虑以下方面: -_projection(torch.cat((h_t, context), dim=1)) # (batch_size, hidden_size) combined_output = torch.tanh 语言:选择适合你的业务场景的编程语言,比如 Java、Go、Python 等。 -(combined_output) target_vocab_dist = self.target_vocab_projection(combined_output) # (batch_size, tgt_vocab_size) 框架:选择适合你的业务场景的框架,比如 Spring Cloud、Dubbo、ServiceComb 等。 - topk_probs, topk_idx = target_vocab_dist.topk(beam_size, dim=1) # (batch_size, beam_size 中间件:选择适合你的业务场景的中间件,比如 Redis、MySQL、Kafka 等。 3) for i in range(beam_size): new_hypo = Hypothesis(h.value + [topk_idx[0][i. 基础设施搭建: 在搭建微服务基础设施时,需要考虑以下方面: - 注册].item()], h.score + topk_probs[0][i].item()) all_hypo.append(new_hypo) # sort hypothesis中心:用于服务注册和发现,比如 EurekaConsul 等。 - 配置中心:用于配置 by descending score and select top k sorted_hypo = sorted(all_hypo, key=lambda h: h.score, reverse=True) hypo = sorted_hypo[:beam_size] # check if all hypothesis have completed if all([h.value[-1]管理,比如 Spring Cloud Config、Apollo 等。 - 网关:用于 API 网关和请求转发,比如 Zuul == self.vocab.tgt['</sGateway 等。 - 链路追踪:用于分布式链路追踪和性能监控,比如 Zipkin、SkyWalking 等。 4. 业务实现: 在实现微服务业务逻辑时,需要考虑以下方面: - 服务开发:编写业务逻辑代码,并进行单元测试和集成测试。 - 服务部署:将服务部署到云平台或服务器上,并进行系统测试和性能测试。 - 服务监控:对服务进行监控和管理,包括日志管理、指标监控、告警等。 以上是搭建一个微服务项目的主要步骤,需要注意的是,在实际项目中,还需要考虑很多其他方面的问题,比如安全、容灾、自动化运维等。如果你需要更多帮助,可以提出具体问题,我会尽力帮助你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值